//
//  GSCCommonRegisterBindVerifyController.m
//  GSCalendar
//
//  Created by guoshikeji_a on 2018/9/10.
//  Copyright © 2018年 tanyin. All rights reserved.
//

#import "GSCCommonRegisterBindVerifyController.h"

#import "NSString+RegexCategory.h"
#import "NSString+GSCommonLogin.h"


#import "GSCommonLoginVerificationCodeView.h"
#import "GSCommonLoginVerificationCodeButton.h"

#import "GSCommonLoginSuccessController.h"


@interface GSCCommonRegisterBindVerifyController ()

@property (nonatomic, strong) GSCommonLoginVerificationCodeView *verifyCodeView;

/**
 *GSCommonLoginVerificationCodeButton
 */
@property (nonatomic, strong) GSCommonLoginVerificationCodeButton *codeButton;

@end

@implementation GSCCommonRegisterBindVerifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.largeTitleLabel.text = GSLoginLocalized(@"gs_login_verify_code_title");
    
    NSString *account = [GSCommonLoginDataManager sharedData].bindingModel.username;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    
    NSString *subStr;
    //语言
    NSString *LocaleIdentifier = @"";
    if ([LocaleIdentifier isEqualToString:@"zh-Hans"]) {//中文
        if (type==GSCCommonLoginAccountTypePhone) {
            account = [NSString phoneNumberPrivacyFrom:account];
            subStr = [NSString stringWithFormat:@"请通过%@手机号获取6位数字验证码",account];//
        }else {
            subStr = [NSString stringWithFormat:@"请通过%@邮箱获取6位数字验证码",account];
        }
    }else {
        if (type==GSCCommonLoginAccountTypePhone) {
            subStr = [NSString stringWithFormat:@"Please get the 6 digits captcha by phone-%@",account];
        }else {
            subStr = [NSString stringWithFormat:@"Please get the 6 digits captcha by email-%@",account];
        }
    }
    NSMutableAttributedString *attriText = [[NSMutableAttributedString alloc] initWithString:subStr];
    NSRange range = [subStr rangeOfString:account];
    if (range.location!=NSNotFound) {
        [attriText addAttributes:@{NSForegroundColorAttributeName:GSLOGIN_THEME_COLOR} range:range];
    }
    self.subTitleLabel.attributedText = attriText;
    [self setupSubviews];
    //
    __weak typeof(self) wself = self;
    self.verifyCodeView.verifyCodeComplete = ^(BOOL compelte) {
        if (wself.doneButton.enabled!=compelte) {
            wself.doneButton.enabled = compelte;
        }
    };
    
    //开始倒计时
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusWaiting;
    
    //唤起输入
    [self.verifyCodeView beginInputAction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupSubviews {
    self.verifyCodeView = [GSCommonLoginVerificationCodeView new];
    self.verifyCodeView.verifyCodeDigits = 6;
    [self.view addSubview:self.verifyCodeView];
    //
    CGSize size = [GSCommonLoginVerificationCodeView viewSizeWithVerifyCodeDigits:self.verifyCodeView.verifyCodeDigits];
    [self.verifyCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(30.0);
        make.left.mas_equalTo(self.view).offset(21.0);
        make.size.mas_equalTo(size);
    }];
    //
    self.codeButton = [GSCommonLoginVerificationCodeButton new];
    [self.view addSubview:self.codeButton];
    [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.verifyCodeView.mas_bottom).offset(16.0);
        make.left.mas_equalTo(self.verifyCodeView);
        
    }];
    [self.codeButton addTarget:self action:@selector(codeButtonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    //
    [self.view addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeButton.mas_bottom).offset(76.0/2);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
    self.doneButton.enabled = false;
    //
    [self.view layoutIfNeeded];
}

#pragma mark - 发送验证码
- (void)codeButtonDidClickedAction:(QMUIButton *)button {
    [self.view endEditing:true];
    NSString *account = [GSCommonLoginDataManager sharedData].bindingModel.username;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    if (type==GSCCommonLoginAccountTypePhone) {
        [self requestPhoneCaptcha:account];
    }else {
        [self requestEmailCaptchaAddress:account];
    }
}
//发送手机验证码
- (void)requestPhoneCaptcha:(NSString *)phone {
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusSending;
    
}
//发送邮箱验证码
- (void)requestEmailCaptchaAddress:(NSString *)email {
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusSending;
}

#pragma mark -
- (void)doneButtonDidClickedAction {
    [self.view endEditing:true];
    //
    [self requestRegisterAction];
}

#pragma mark -
- (void)requestRegisterAction {
    NSString *account = [GSCommonLoginDataManager sharedData].bindingModel.username;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    if (type==GSCCommonLoginAccountTypePhone) {
        [self requestPhoneRegisterAction:account];
    }else {
        [self requestEmailRegisterAction:account];
    }
}
//手机号码注册
- (void)requestPhoneRegisterAction:(NSString *)phone {
    [self.view showLoginToast];
}
//邮箱注册
- (void)requestEmailRegisterAction:(NSString *)email {
    [self.view showLoginToast];
}



#pragma mark - 注册成功返回首页
- (void)registerSuccessToHomeAction {
    __weak typeof(self) wself = self;
    [GSCommonLoginSuccessController showFromController:self succssType:GSCommonLoginSuccessTypeRegisterSuccuss complete:^{
        [wself.navigationController dismissViewControllerAnimated:true completion:^{
            [wself.navigationController popToRootViewControllerAnimated:true];
            [[NSNotificationCenter defaultCenter] postNotificationName:UserCommonLoginSuccessNotification object:nil];
        }];
        //触发绑定邀请码
        [[GSCommonLoginDataManager sharedData] userRegisterSuccessBindInvatationCode];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:true];
}

@end
